package com.mall.untils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.security.auth.callback.LanguageCallback;

/**
 * 日期工具类
 */
public class DateUtil {

    public static void main(String[] args) {
        long todayLong = getTodayLong();
        System.out.println(todayLong);

        System.out.println(getDateByMis(1431933704000L));

        System.out.println(getTodayString());


    }

    /**
     * 将日期串转为日期对象
     *
     * @param dateString
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date getDateFromString(String dateString, String format) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(dateString);
        } catch (Exception e) {
            System.out.println(e);
        }

        /**
         * 异常就返回当前时间
         */
        return new Date();
    }


    /**
     * 将date格式的数据转为字符串格式的日期数据
     *
     * @param date
     * @param format
     * @return
     */
    public static String getDateString(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }


    /**
     * 获取今天日期串
     *
     * @return
     */
    public static String getTodayString() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT_DAY);
        return simpleDateFormat.format(new Date());
    }


    /**
     * 获取昨天日期串
     *
     * @return
     */
    public static String getYesterdayString() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT_DAY);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return simpleDateFormat.format(calendar.getTime());
    }


    /**
     * 获取当前时间的日期数，即从1970年到今天是第几天了
     *
     * @return
     */
    public static long getTodayIsDays() {
        long date = System.currentTimeMillis();
        return date / 86400000;
    }


    /**
     * 获取当前时间的日期数，即从1970年到今天是第几天了
     *
     * @return
     */
    public static long getTodayLong() {
//        long date = System.currentTimeMillis();
//        return date / 86400000 * 86400000;
        Calendar currentDate = new GregorianCalendar();

        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);

        return currentDate.getTime().getTime();
    }


    public static String getDateByMis(long m) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(m);
        return formatter.format(calendar.getTime());
    }

    /**
     * 根据Long型的时间，转化为Time
     *
     * @param time
     * @return
     */
    public static Timestamp getTimestampByLong(long time) {
        return new Timestamp(time);
    }

    /**
     * @param timestamp
     * @return
     */
    public static long getLongByTimestamp(Timestamp timestamp) {
        return timestamp.getTime();
    }

}
